package it.polimi.travelDream.ejb.servlet;

import it.polimi.travelDream.ejb.interfaces.PackageMgrRemote;
import it.polimi.travelDream.ejb.interfaces.TransportationMgrRemote;

import java.io.IOException;
import java.util.Map;
import java.lang.Object;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TravelServlet
 */
@WebServlet("/DelTransportation")
public class DelTravel extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	TransportationMgrRemote tmgr;
	@EJB
	PackageMgrRemote pmr;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DelTravel() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// TODO Auto-generated method stub
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Map<String, String[]> mappa = request.getParameterMap();
		
		if(pmr.delReturn(Integer.parseInt(mappa.get("idTransportation")[0])) || pmr.delReturn(Integer.parseInt(mappa.get("idTransportation")[0])) ){
			request.setAttribute("message", "This excursion is part of a package. It cannot be removed");
			request.getRequestDispatcher("/BPAdvice.jsp").forward(request, response); 
		}
		else{
		tmgr.delTransportation(Integer.parseInt(mappa.get("idTransportation")[0]));
		request.setAttribute("message", "Travel succesfully deleted.");
		request.getRequestDispatcher("/BPAdvice.jsp").forward(request, response); 
		
	}

}
	}
